package com.qing.demo.netty.util;

import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author caozhipeng
 * @date 2025/8/7
 */
public class NetUtil {

    public static int getPort(){
        int initPort=7379;
        while (true){
            if(!isPortUsing(initPort)){
                break;
            }
            initPort++;
        }
        return initPort;
    }

    public static boolean isPortUsing(int port){
        boolean flag = false;
        try {
            Socket socket=new Socket("127.0.0.1",port);
            socket.close();
            flag=true;
        } catch (Exception e) {
        }
        return flag;
    }

    public static String getHost() throws UnknownHostException {
      return InetAddress.getLocalHost().getHostAddress();
    }

    public static void main(String[] args) throws UnknownHostException {
        System.out.println(getHost());
    }
}
